<?php
/**
 * communit.as
 * @copyright (C)2008 Jaybill McCarthy, All Rights Reserved.
 * @category communitas
 * @package communitas
 * @author Jaybill McCarthy
 * @link http://communit.as communit.as
 * @license http://communit.as/docs/license License
 */

/**
 *
 * @package communitas
 * @subpackage forum_controllers
 * @license http://communit.as/docs/license License 
 */

class Forum_PostController extends Forum_Controller_Action_Abstract {

	function init(){	

		parent::init();

	}	

	function deleteAction(){
		$request = new Cts_Request($this->getRequest());
		$posts_table = new Posts();
		$topics_table = new Topics();
		$topic = null;

		if($request->has('id')){
			$post = $posts_table->fetchRow('id = '.$request->id);						
			if(is_null($post)){
				$this->_redirect("/default/auth/missing");
			} else {
				$filter = new FilterBBCode();				
				$this->view->text = $filter->filter($post->post);
				$this->view->d_url = "/forum/post/delete/id/".$post->id;				
				if($post->is_topic == 1){
					$topic = $topics_table->fetchRow('id = '.$post->topic_id);
					if(!is_null($topic)){
						$this->view->title = $topic->title;
					}					
					$post_count = $posts_table->getCountByWhereClause('topic_id = '.$post->topic_id);
				}
			}
		}		
		$errors = array();

		if(!$this->_identity->isAdmin){
			if($post->username != $this->_identity->username){	
				$errors[] = $this->_T("You can't delete this post because it isn't yours.");
			}
		}

		if(!is_null($topic)){
			if($topic->status == "locked" and !$this->_identity->isAdmin){
				$errors[] = $this->_T("You can't delete this post because it is locked.");
			}
			if($post_count > 1 and !$this->_identity->isAdmin){
				$errors[] = $this->_T("You can't delete this topic because it already has replies.");
			}
		}

		if($this->_request->isPost() and count($errors) == 0){
			$confirm = $this->_request->getParam('delete', null);
			if($confirm == "Yes" and !is_null($request->id)){
				if($post->is_topic == 1){
					$topics_table->delete('id = ' . $post->topic_id);
					$posts_table->delete('topic_id = ' . $post->topic_id);
				} else {
					$posts_table->delete('id = ' . $post->id);
				}
			}
			if($post->is_topic){
				$this->_redirect('/forum/forum/index/id/' . $topic->forum_id);
			} else {
				$this->_redirect('/forum/topic/index/id/' . $post->topic_id);
			}
		}
		$this->view->errors = $errors;

	}

	function editAction(){
		$request = new Cts_Request($this->getRequest());		
		$request->stripTags(array('title','post'));	
		$this->view->statuses = array('active' => 'Active','locked' => 'Locked','inactive' => 'Hidden');	
		$forums_table = new Forums();
		$topics_table = new Topics();
		$posts_table = new Posts();		
		$all_forums = $forums_table->fetchAll(null,'title');
		$tmp_forums = array();		
		foreach($all_forums as $forum){
			$tmp_forums[$forum->id] = $forum->title;
		}
		$this->view->forums = $tmp_forums;
		$forum = null;
		$post = null;
		$topic = null;
		// determine if we're editing an existing post
		if($request->has('id')){
			$post = $posts_table->fetchRow('id = ' . $request->id);
			if(!is_null($post)){							
				$this->view->post = $post->post;
				$this->view->id = $post->id;
				if($post->is_topic == 1){
					$topic = $topics_table->fetchrow('id = '.$post->topic_id);
					if(!is_null($topic)){
						
						$d_forum_where = $forums_table->getAdapter()->quoteInto('id = ?',$topic->forum_id);
						$display_forum = $forums_table->fetchRow($d_forum_where);				
						if($topic->username == $this->_identity->username or $this->_identity->isAdmin){					
							$this->view->mode = "edit_topic";
							$this->view->pagetitle = "Edit Topic";
							$this->view->forumid = $topic->forum_id;
							$this->view->forum = $display_forum->toArray();
							$this->view->topic = $topic->toArray();
							$this->view->title = $topic->title;
							$this->view->topicid = $topic->id;
							$this->view->show_topic = true;
							$this->view->status = $topic->status;							
							if((boolean)$topic->sticky){
								$this->view->sticky = true;
							}
						} else {
							$topic = null;
						}
					}
				} else {
					if($post->username == $this->_identity->username or $this->_identity->isAdmin){						
						$display_topic = $topics_table->fetchRow('id = '.$post->topic_id);						
						$d_forum_where = $forums_table->getAdapter()->quoteInto('id = ?',$display_topic->forum_id);
						$display_forum = $forums_table->fetchRow($d_forum_where);						
						$this->view->forum = $display_forum->toArray();
						$this->view->topic = $display_topic->toArray();									
						$this->view->mode = "edit_post";
						$this->view->pagetitle = "Edit Post";
					} else {
						$post = null;
					}
				}
			}
		}elseif($request->has('forumid')) { // we don't have a post id, maybe we have a forum id?
			$where = $forums_table->getAdapter()->quoteInto('id = ?',$request->forumid);
			$forum = $forums_table->fetchRow($where);
			if(!is_null($forum)){
				$this->view->forumid = $forum->id;		
				$this->view->show_topic = true;
				$this->view->pagetitle = "New Topic";
				$this->view->forum = $forum->toArray();
				$this->view->mode = "new_topic";	
			}
		}elseif($request->has('topicid')){// we don't have forum or a post, maybe we have a topic?
			$topic = $topics_table->fetchRow('id = '.$request->topicid);
			if(!is_null($topic)){
				$this->view->topicid = $topic->id;
				$d_forum_where = $forums_table->getAdapter()->quoteInto('id = ?',$topic->forum_id);
				$display_forum = $forums_table->fetchRow($d_forum_where);
				$this->view->topic = $topic->toArray();
				$this->view->forum = $display_forum->toArray();	
				$this->view->mode = "new_reply";
				$this->view->pagetitle = "Reply to Topic";
				if($request->has('quotepostid')){
					$quotepost_where = $posts_table->getAdapter()->quoteInto('id = ?',$request->quotepostid);
					$quote_post = $posts_table->fetchRow($quotepost_where);					
					if(!is_null($quote_post)){
						$quote = str_replace("\n"," ",trim($quote_post->post));			
						$this->view->post = "[quote=".$quote_post->username."]".$quote."[/quote]\n";
					}
				}
			}	
		} 
		
		if(!is_null($topic)){			
			if($this->_auth->hasIdentity())
			{
				if(!$this->_identity->isAdmin){
					if($topic->status == "locked"){				
						// topic is locked
						$this->_redirect('/forum/topic/index/id/'.$topic->id);
					} elseif ($topic->status == "inactive"){				
						// topic is inactive
						$this->_redirect("/default/auth/missing");
					}
				}
			}
		}
		
		if(is_null($forum) and is_null($post) and is_null($topic)){
			//we got nothin'. redirect
			$this->_redirect('/forum'); 
		}
		
		if ($this->_request->isPost()) {
			$errors = array();						
			$post_data = array('post' => $request->post);
			if ($request->has('sticky')) {
				$sticky = (int)$request->sticky;
			} else {
				$sticky = 0;
			}
			$post_min = Cts_Registry::get('forum_post_min_length', 'forum');
			$post_max = Cts_Registry::get('forum_post_max_length', 'forum');
			$topic_min = Cts_Registry::get('forum_topic_min_length', 'forum');
			$topic_max = Cts_Registry::get('forum_topic_max_length', 'forum');

			if (!Cts_Validate::checkLength($request->post, $post_min, $post_max)) {
				$errors[] = $this->_T("Post text must be between %s and %s", array($post_min, $post_max));
			}
			$preview = false;
			if ($request->Submit == "Preview") {
				$preview = true;
				$this->view->preview = $tmp_post['post'] = $this->_filter->filter($request->post);
			}
			switch ($request->mode) {
				case "edit_topic":
					if($topic->username == $this->_identity->username or $this->_identity->isAdmin){
						$topic_data = array(
							'title' => $request->title,						
						);
						if($this->_identity->isAdmin){
							$topic_data['status'] = $request->status;							
							$topic_data['sticky'] = $sticky;
							$topic_data['forum_id'] = $request->move_to_forumid;
						}

						if(!Cts_Validate::checkLength($request->title,$topic_min,$topic_max)){
							$errors[] = $this->_T("Topic title must be between %s and %s characters", array($topic_min, $topic_max));
						}
						if(count($errors) == 0 and !$preview){					
							$topics_table->update($topic_data,"id = " . $topic->id);
							$this->_cts_plugin->doAction('forum_topic_post_update',array('topic_id' => $topic->id));
							$posts_table->update($post_data,'id = '.$request->id);					
							$this->_cts_plugin->doAction('forum_post_post_update',array('post_id' => $request->id));
							$this->_redirect('/forum/topic/index/page/last/id/'.$post->topic_id."#last-post");
						} else {							
							$this->view->title = $request->title;
							$this->view->status = $request->status;
							$this->view->post = $request->post;
							$this->view->sticky = $sticky;														
							$this->view->errors = $errors;
						}
					}
				break;

				case "new_topic":

					$topic_data = array(
						'title' 	=> $request->title,					
						'username' 	=> $this->_identity->username,
						'forum_id' 	=> $forum->id,
					);

					if($this->_identity->isAdmin){
						$topic_data['status'] = $request->status;							
						$topic_data['sticky'] = $sticky;
					}

					if(!Cts_Validate::checkLength($request->title, $topic_min, $topic_max)){
						$errors[] = $this->_T("Topic title must be between %s and %s characters", array($topic_min, $topic_max));
					}
					if(count($errors) == 0 and !$preview){
						$post_data['topic_id'] = $topics_table->insert($topic_data);
						$this->_cts_plugin->doAction('forum_topic_post_insert',array('topic_id' => $post_data['topic_id']));
						// set the post as "is topic" so we can edit it later with the post				
						$post_data['is_topic'] = 1;					
						$post_data['username'] = $topic_data['username'];						
						$post_data = $this->_cts_plugin->doFilter('forum_post_pre_insert', $post_data);
						$post_id = $posts_table->insert($post_data);
						$this->_cts_plugin->doAction('forum_post_post_insert', array('post_id' => $post_id));
						$this->_redirect('/forum/topic/index/page/last/id/'.$post_data['topic_id']."#last-post");
					} else {
						$this->view->title = $request->title;
						$this->view->status = $request->status;
						$this->view->post = $request->post;							
						$this->view->sticky = $sticky;							
						$this->view->errors = $errors;
					}
				break;

				case "edit_post":
					if(count($errors) == 0 and !$preview){
						if($post->username == $this->_identity->username or $this->_identity->isAdmin){										
							$posts_table->update($post_data,'id = '.$request->id);
							$this->_redirect('/forum/topic/index/page/last/id/'.$post->topic_id."#last-post");
						}
					} else {
						$this->view->post = $request->post;					
						$this->view->errors = $errors;
					} 
				break;
				
				case "new_reply":
					if(count($errors) == 0 and !$preview){
						$post_data['username'] = $this->_identity->username;
						$post_data['topic_id'] = $request->topicid;
						$post_data = $this->_cts_plugin->doFilter('forum_post_pre_insert',$post_data);
						$post_id = $posts_table->insert($post_data);
						$this->_cts_plugin->doAction('forum_post_post_insert',array('post_id' => $post_id));					
						$this->_redirect('/forum/topic/index/page/last/id/'.$post_data['topic_id']."#last-post");
					} else {
						$this->view->post = $request->post;					
						$this->view->errors = $errors;
					}
				break;
			}
						
		}		
		
	}
	
}